﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using QxFramework.Core;
using UnityEngine.EventSystems;
using UnityEngine.UI;
/// <summary>
///   *输入管理器
/// </summary>
/// 

public class InputManager : MonoBehaviour
{
    #region Mono Function
    public static InputManager Instance = null;
    void Awake()
    {
        if (Instance == null)
        {
            Instance = this;
            //DontDestroyOnLoad(this.gameObject);
        }
        else
        {
            if (Instance != this)
            {
                Destroy(transform.gameObject);
            }
        }
    }
    #endregion

    public bool Up(int playerID)
    {
        if(playerID == 0)
        {
            return Input.GetKey("w");
        }
        else
        {
            return Input.GetAxis("MoveY") > 0.1 || Input.GetAxis("Skill34") > 0.8;
        }
    }
    public bool Down(int playerID)
    {
        if (playerID == 0)
        {
            return Input.GetKey("s");
        }
        else
        {
            return Input.GetAxis("MoveY") < -0.1 || Input.GetAxis("Skill34") < -0.8;
        }
    }
    public bool Left(int playerID)
    {
        if (playerID == 0)
        {
            return Input.GetKey("a");
        }
        else
        {
            return Input.GetAxis("MoveX") > 0.1 || Input.GetAxis("Skill12") < -0.8;
        }
    }
    public bool Right(int playerID)
    {
        if (playerID == 0)
        {
            return Input.GetKey("d");
        }
        else
        {
            return Input.GetAxis("MoveX") < -0.1 || Input.GetAxis("Skill12") > 0.8;
        }
    }
    public bool Port(int PlayerID)
    {
        if (PlayerID == 0)
        {
            return Input.GetKeyDown("space");
        }
        if(PlayerID == 1)
        {
            return Input.GetKeyDown(KeyCode.JoystickButton0);
        }
        return false;
    }
    public bool ESC(int playerID)
    {
        if (playerID == 0)
        {
            return Input.GetKeyDown(KeyCode.Escape);
        }
        else
        {
            return Input.GetKeyDown(KeyCode.JoystickButton7);
        }
    }
    public bool NextSkill(int playerID)
    {
        if (playerID == 0)
        {
            return Input.GetKeyDown("e") || Input.GetKeyDown(KeyCode.Mouse3) || Input.GetAxis("Mouse ScrollWheel") > 0;
        }
        else
        {
            return Input.GetKeyDown(KeyCode.JoystickButton5);
        }
    }
    public bool PreviousSkill(int playerID)
    {
        if (playerID == 0)
        {
            return Input.GetKeyDown("q") || Input.GetKeyDown(KeyCode.Mouse4) || Input.GetAxis("Mouse ScrollWheel") < 0;
        }
        else
        {
            return Input.GetKeyDown(KeyCode.JoystickButton4);
        }
    }

    public bool ClickFight(out Vector3 pos, int playerID)
    {
        pos = GetTarget(playerID);
        if (GetOverUI())
        {
            return false;
        }
        if (playerID == 0)
        {
            return Input.GetMouseButton(1);
        }
        else
        {
            if (Input.GetAxis("AttackX") == 0 && Input.GetAxis("AttackY") == 0)
                return false;
            return Input.GetAxis("TriggerLeft") > 0.5;
        }
    }

    public bool ClickFire(out Vector3 pos, int playerID)
    {
        pos = GetTarget(playerID);
        if (GetOverUI())
        {
            return false;
        }
        if (playerID == 0)
        {
            return Input.GetMouseButton(0);
        }
        else
        {
            if (Input.GetAxis("AttackX") == 0 && Input.GetAxis("AttackY") == 0)
                return false;
            return Input.GetAxis("TriggerRight") > 0.5;
        }

    }
    public bool ClickRoll(out Vector3 pos, int playerID)
    {
        if (playerID == 0)
        {
            pos = GetTarget(playerID);
            return Input.GetMouseButtonDown(2);
        }
        else
        {
            pos = new Vector3(-100 * Input.GetAxis("MoveX"), 100 * Input.GetAxis("MoveY"), 0);
            if (Input.GetAxis("MoveX") == 0 && Input.GetAxis("MoveY") == 0)
            {
                if (Input.GetAxis("Skill34") > 0.8)
                {
                    pos = new Vector3(0, 100, 0);
                }
                if (Input.GetAxis("Skill34") < -0.8)
                {
                    pos = new Vector3(0, -100, 0);
                }
                if (Input.GetAxis("Skill12") < -0.8)
                {
                    pos = new Vector3(-100, 0, 0);
                }
                if (Input.GetAxis("Skill12") > 0.8)
                {
                    pos = new Vector3(100, 0, 0);
                }
            }
            pos += GameMgr.Get<IPlayerDataManager>().GetPlayerDatas().PlayerDatas[playerID].PlayerMono.Position;
            return Input.GetKeyDown(KeyCode.JoystickButton2);
        }
    }

    public bool Cheat
    {
        get
        {
            return Input.GetKey("z") && Input.GetKey("p");
        }
    }

    public Vector3 GetTarget(int playerID)
    {
        if (playerID == 0)
        {
            Vector3 screenPosition = Camera.main.WorldToScreenPoint(transform.position);
            //获取鼠标在场景中坐标
            Vector3 mousePositionOnScreen = Input.mousePosition;
            //让场景中的Z=鼠标坐标的Z
            mousePositionOnScreen.z = screenPosition.z;
            //将相机中的坐标转化为世界坐标
            Vector3 mousePositionInWorld = Camera.main.ScreenToWorldPoint(mousePositionOnScreen);
            Vector3 pos = new Vector3(mousePositionInWorld.x, mousePositionInWorld.y, mousePositionInWorld.z);
            return pos;
        }
        else
        {
            Vector3 pos = new Vector3(100 * Input.GetAxis("AttackX"), 100 * Input.GetAxis("AttackY"), 0);
            pos += GameMgr.Get<IPlayerDataManager>().GetPlayerDatas().PlayerDatas[playerID].PlayerMono.Position;
            return pos;
        }
    }

    public GameObject raycaster;
    /// <summary>
    /// 获取鼠标停留处物体
    /// </summary>
    /// <param name="raycaster"></param>
    /// <returns></returns>
    public GameObject GetOverGameObject()
    {
        PointerEventData pointerEventData = new PointerEventData(EventSystem.current);
        pointerEventData.position = Input.mousePosition;
        PhysicsRaycaster pr = raycaster.GetComponent<Physics2DRaycaster>();
        List<RaycastResult> results = new List<RaycastResult>();
        pr.Raycast(pointerEventData, results);
        if (results.Count != 0)
        {
            return results[0].gameObject;
        }
        return null;
    }
    public GameObject canvas;
    /// <summary>
    /// 获取鼠标停留处UI
    /// </summary>
    /// <param name="canvas"></param>
    /// <returns></returns>
    public bool GetOverUI()
    {
        PointerEventData pointerEventData = new PointerEventData(EventSystem.current);
        pointerEventData.position = Input.mousePosition;
        GraphicRaycaster gr = canvas.GetComponent<GraphicRaycaster>();
        List<RaycastResult> results = new List<RaycastResult>();
        gr.Raycast(pointerEventData, results);
        if (results.Count != 0)
        {
            return true;
        }
        return false;
    }

    public bool AttackAxis(int playerID)
    {
        if (playerID == 0)
        {
            return true;
        }
        if (Input.GetAxis("AttackX") == 0 && Input.GetAxis("AttackY") == 0)
        {
            return false;
        }
        else
        {
            return true;
        }
    }
}
